package com.easondongh.blog.controller;

import com.easondongh.blog.enums.ResultEnum;
import com.easondongh.blog.pojo.Blog;
import com.easondongh.blog.pojo.BlogCollection;
import com.easondongh.blog.pojo.BlogGoods;
import com.easondongh.blog.pojo.User;
import com.easondongh.blog.service.BlogService;
import com.easondongh.blog.utils.Page;
import com.easondongh.blog.utils.Result;
import com.easondongh.blog.utils.ShiroUtils;
import com.easondongh.blog.utils.StringUtils;
import com.easondongh.blog.vo.BlogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 博客主体
 * @author EasonDongH
 * @date 2020/3/26 21:20
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    /**
     * 新增博客
     * @param blog
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<Object> save(@RequestBody Blog blog) {
        blogService.save(blog);
        return new Result<>(ResultEnum.BLOG_SAVE_SUCCESS);
    }

    /**
     * 根据id查询
     */
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Result<Blog> get(@PathVariable String id) {
        Blog blog = blogService.getById(id);
        return new Result<>(blog);
    }

    /**
     * 更新博客信息
     * @param blog
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result<Object> update(@RequestBody Blog blog) {
        blogService.update(blog);
        return new Result<>("更新成功");
    }

    /**
     * 根据id查找博客
     * @param id
     * @return
     */
    @RequestMapping(value = "/read/{id}", method = RequestMethod.GET)
    public Result<BlogVo> read(@PathVariable String id) {
        BlogVo blog = blogService.readById(id);
        return new Result<>(blog);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Result<Object> delete(@PathVariable String id) {
        blogService.deleteById(id);
        return new Result<>("删除成功");
    }

    /**
     * 分页查询
     * @param page
     * @return
     */
    @RequestMapping(value = "/getByPage", method = RequestMethod.POST)
    public Result<Page<BlogVo>> getByPage(@RequestBody Page<BlogVo> page) {
        String sortColumn = page.getSortColumn();
        if (StringUtils.isNotBlank(sortColumn)) {
            // 排序列不为空
            String[] sortColumns = {"blog_goods", "blog_read", "blog_collection",
                    "type_name", "blog_comment", "created_time", "update_time"};
            List<String> sortList = Arrays.asList(sortColumns);
            if (!sortList.contains(sortColumn.toLowerCase())) {
                return new Result<>(ResultEnum.PARAMS_ERROR.getCode(), "排序参数不合法");
            }
        }
        page = blogService.getByPage(page);
        return new Result<>(page);
    }

    /**
     * 获取推荐阅读
     * @return
     */
    @RequestMapping(value = "/recomRead", method = RequestMethod.GET)
    public Result<List<Blog>> getRecommend() {
        List<Blog> blogList = this.blogService.getRecommend();
        return new Result<>(blogList);
    }

    /**
     * 获取文章时间轴，仅返回博客标题、创建时间
     * @return
     */
    @RequestMapping(value = "/getTimeLine", method = RequestMethod.GET)
    public Result<List<Blog>> getTimeline() {
        List<Blog> blogList = this.blogService.getTimeline();
        return new Result<>(blogList);
    }

    /**
     * 文章点赞
     * @param blogGoods
     * @return
     */
    @RequestMapping(value = "/good", method = RequestMethod.POST)
    public Result<ResultEnum> good(@RequestBody BlogGoods blogGoods) {
        if(StringUtils.isNotBlank(blogGoods.getBlogId())) {
            User loginUser = (User) ShiroUtils.getLoginUser();
            if(loginUser != null) {
                blogGoods.setUserId(loginUser.getUserId());
                this.blogService.goodByBlogAndUser(blogGoods);
            }
        }
        return new Result<>(ResultEnum.SUCCESS);
    }

    /**
     * 判断当前用户是否已给当前文章点赞
     * @param blogId
     * @return
     */
    @RequestMapping(value = "/hasGood/{id}", method = RequestMethod.GET)
    public Result<Boolean> hasGood(@PathVariable("id") String blogId) {
        boolean hasGood = false;
        if(StringUtils.isNotBlank(blogId)) {
            User loginUser = (User) ShiroUtils.getLoginUser();
            if(loginUser != null) {
                hasGood = this.blogService.hasGoodByBlogAndUser(loginUser.getUserId(), blogId);
            }
        }
        return new Result<>(hasGood);
    }

    /**
     * 文章收藏
     * @param blogCollection
     * @return
     */
    @RequestMapping(value = "/collect", method = RequestMethod.POST)
    public Result<ResultEnum> collect(@RequestBody BlogCollection blogCollection) {
        if(StringUtils.isNotBlank(blogCollection.getBlogId())) {
            User loginUser = (User) ShiroUtils.getLoginUser();
            if(loginUser != null) {
                blogCollection.setUserId(loginUser.getUserId());
                blogCollection.setUser(loginUser);
                this.blogService.collectByBlogAndUser(blogCollection);
            }
        }
        return new Result<>(ResultEnum.SUCCESS);
    }

    /**
     * 判断当前用户是否已收藏当前文章
     * @param blogId
     * @return
     */
    @RequestMapping(value = "/hasCollect/{id}", method = RequestMethod.GET)
    public Result<Boolean> hasCollect(@PathVariable("id") String blogId) {
        boolean hasCollect = false;
        if(StringUtils.isNotBlank(blogId)) {
            User loginUser = (User) ShiroUtils.getLoginUser();
            if(loginUser != null) {
                hasCollect = this.blogService.hasCollectByBlogAndUser(loginUser.getUserId(), blogId);
            }
        }
        return new Result<>(hasCollect);
    }
}
